<template>
  <div>
    <div style="padding: 5px 0">


        <el-input v-model="text" style="width: 200px" clearable> </el-input>
        <el-button type="primary" size="mini" @click="load" style="margin-left: 10px">查询</el-button>

        <el-button @click="add" type="primary" size="mini" style="margin: 10px 10px">新增</el-button>
    </div>
    <el-table :data="tableData"style="width: 100%">
      <el-table-column prop="id" label="ID" width="100"> </el-table-column>
      <el-table-column prop="title" label="标题" show-overflow-tooltip> </el-table-column>
      <el-table-column prop="content" label="内容" show-overflow-tooltip> </el-table-column>
<!--      <el-table-column prop="time" label="发布时间"> </el-table-column>-->

      <el-table-column
          fixed="right"
          label="操作"
          width="200">
        <template slot-scope="scope">
            <el-button
                size="mini"
                @click="edit(scope.row)">编辑</el-button>
            <el-popconfirm
                @confirm="del(scope.row.id)"
                title="你确定删除吗？"
            >
            <el-button
                size="mini"
                type="danger" slot="reference" style="margin-left:20px"
               >删除</el-button>
            </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum" background
        :page-size="pageSize"
        :page-sizes="[2, 5, 10]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 弹窗   -->
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%"
               :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false">
      <el-form :model="entity">
        <el-form-item label="标题" label-width="100px">
          <el-input v-model="entity.title" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="内容" label-width="100px">
          <el-input type="textarea" :rows="5" v-model="entity.content" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import API from '../../utils/request'
const url = "/api/bulletin/"

export default {
  name: "Notice",
  data() {
    return {
      user: {},
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      entity: {},
      dialogFormVisible: false,
      total: 0, text: '',

    };
  },
  created() {
    this.user = sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {}
    this.load()
  },
  methods: {
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    load() {
       API.get(url + "/page", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            name: this.text
          }
       }).then(res => {
          this.tableData = res.data.records || []
          this.total = res.data.total
       })
    },
    add() {
      this.entity = {}
      this.dialogFormVisible = true
    },
    edit(obj) {
      this.entity = JSON.parse(JSON.stringify(obj))
      this.dialogFormVisible = true
    },
    save() {
      if (!this.entity.id) {
        API.post(url, this.entity).then(res => {
          this.$message({
            type: "success",
            message: "操作成功"
          })
          this.load()
          this.dialogFormVisible = false
        })
      } else {
        API.put(url, this.entity).then(res => {
          this.$message({
            type: "success",
            message: "操作成功"
          })
          this.load()
          this.dialogFormVisible = false
        })
      }

    },
    del(id) {
      API.delete(url + id).then(res => {
        this.$message({
          type: "success",
          message: "操作成功"
        })
        this.load()
      })
    }
  },
};
</script>

<style scoped>
</style>
